<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="common :: head('登录')">
</head>
<body>
<div id="app">
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img th:src="@{img/logo.png}" alt="">
            </div>
            <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="login_form">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button id="btn" type="primary" @click="submitForm('loginForm')">提交</el-button>
                    <el-button @click="resetForm('loginForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>

</div>
</body>

<script>
    new Vue({
        el: '#app',
        data() {

            var validateUserName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    if (this.loginForm.password !== '') {
                        this.$refs.loginForm.validateField('password');
                    }
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { validator: validateUserName, trigger: 'blur' }
                    ],
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        const result = await axios.post('/login', 'username=' + this.loginForm.username + '&password=' + this.loginForm.password)
                        const datas = result.data
                        if (datas.authenticated === true) {
                            sessionStorage.setItem('userId', datas.principal.id)
                            sessionStorage.setItem('nickName', datas.principal.nickname)
                            var authorites = datas.authorities
                            var permission = []
                            for (var i in authorites) {
                                permission.push(authorites[i].authority)
                            }
                            sessionStorage.setItem('permission', JSON.stringify(permission))
                            this.$message.success('登录成功')
                            window.sessionStorage.setItem('token', datas.name)
                            if(permission.indexOf('sys:admin')>-1){
                                top.window.location = '/admin/index';
                                return;
                            }
                            top.window.location = '/';
                            return ;
                        } else {
                            return this.$message.error(result.data.msg)
                        }
                        return
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        mounted () {
            $(document).keydown(function(e){
                if(e.keyCode==13){
                    $("#btn").click();
                }
            })
        }
    })
</script>

<style>
    html{
        background-color: cadetblue;
    }
    .login_container {
        background-color: #2b4b6b;
        height: 100%;
    }

    .login_box {
        height: 300px;
        width: 450px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

    }

    .login_box .avatar_box {
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;

    }
    .avatar_box img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
    .login_form {
        position: absolute;
        padding: 0 20px;
        width: 100%;
        bottom: 0;
        box-sizing: border-box;

    }
    .btns {
        display: flex;
        justify-content: flex-end;
    }
</style>
</html>